<?php  
include "admin_header.php";
include_once( XOOPS_ROOT_PATH.'/class/xoopsblock.php' ) ;
$xoops_system_path = XOOPS_ROOT_PATH . '/modules/system' ;
$xoops_system_url = XOOPS_URL . '/modules/system' ;

// language files
$language = $xoopsConfig['language'] ;
if( ! file_exists( "$xoops_system_path/language/$language/admin/blocksadmin.php") ) $language = 'english' ;

// to prevent from notice that constants already defined
$error_reporting_level = error_reporting( E_ALL ) ;
include_once( "$xoops_system_path/constants.php" ) ;
include_once( "$xoops_system_path/language/$language/admin.php" ) ;
include_once( "$xoops_system_path/language/$language/admin/blocksadmin.php" ) ;
error_reporting( $error_reporting_level ) ;

	
	function clone_block($bid)
    {
		    
        global $xoopsConfig;
        xoops_cp_header();
				
        $myblock = new XoopsBlock($bid);
				$db =& Database::getInstance();
        $sql = 'SELECT module_id FROM '.$db->prefix('block_module_link').' WHERE block_id='.intval($bid);
        $result = $db->query($sql);
        $modules = array();
        while ($row = $db->fetchArray($result)) {
            $modules[] = intval($row['module_id']);
        }
        $is_custom = ($myblock->getVar('block_type') == 'C' || $myblock->getVar('block_type') == 'E') ? true : false;
				$block = array('form_title' => _AM_CLONEBLOCK, 'name' => $myblock->getVar('name'), 'side' => $myblock->getVar('side'), 'weight' => $myblock->getVar('weight'), 'visible' => $myblock->getVar('visible'), 'content' => $myblock->getVar('content', 'N'), 'modules' => $modules, 'is_custom' => $is_custom, 'ctype' => $myblock->getVar('c_type'), 'cachetime' => $myblock->getVar('bcachetime'), 'op' => 'clone_ok', 'bid' => $myblock->getVar('bid'), 'edit_form' => $myblock->getOptions(), 'template' => $myblock->getVar('template'), 'options' => $myblock->getVar('options'));
        //echo '<a href="admin.php?fct=blocksadmin">'. _AM_BADMIN .'</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;'._AM_CLONEBLOCK.'<br /><br />';
        include 'blockform.php';
        $form->display();
        xoops_cp_footer();
        exit();
    }

    function clone_block_ok($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $btitle)
    {
        global $xoopsUser;
        $block = new XoopsBlock($bid);
        $clone =$block->xoopsClone();
        if (empty($bmodule)) {
            xoops_cp_header();
            xoops_error(sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
            xoops_cp_footer();
            exit();
        }
        $clone->setVar('side', $bside);
        $clone->setVar('weight', $bweight);
        $clone->setVar('visible', $bvisible);
				$clone->setVar('content', $bcontent);
        $clone->setVar('title', $btitle);
        $clone->setVar('bcachetime', $bcachetime);
        if ( isset($options) && (count($options) > 0) ) {
            $options = implode('|', $options);
            $clone->setVar('options', $options);
        }
        $clone->setVar('bid', 0);
        if ($block->getVar('block_type') == 'C' || $block->getVar('block_type') == 'E') {
            $clone->setVar('block_type', 'E');
        } else {
            $clone->setVar('block_type', 'D');
        }
				
				$newid = $clone->store();
        if (!$newid) {
            xoops_cp_header();
						$clone->getHtmlErrors();
            xoops_cp_footer();
            exit();
        }
        if ($clone->getVar('template') != '') {
            $tplfile_handler =& xoops_gethandler('tplfile');
            $btemplate =& $tplfile_handler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
            if (count($btemplate) > 0) {
                $tplclone =& $btemplate[0]->xoopsClone();
                $tplclone->setVar('tpl_id', 0);
                $tplclone->setVar('tpl_refid', $newid);
                //$tplman->insert($tplclone);
            }
        }
        $db =& Database::getInstance();
        foreach ($bmodule as $bmid) {
            $sql = 'INSERT INTO '.$db->prefix('block_module_link').' (block_id, module_id) VALUES ('.$newid.', '.$bmid.')';
            $db->query($sql);
        }
        $groups =& $xoopsUser->getGroups();
        $count = count($groups);
        for ($i = 0; $i < $count; $i++) {
            $sql = "INSERT INTO ".$db->prefix('group_permission')." (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (".$groups[$i].", ".$newid.", 1, 'block_read')";
            $db->query($sql);
        }
				redirect_header('myblocksadmin.php',1,_AM_DBUPDATED);
    }

		//print_r($_GET);
		//echo "<hr>";
		//print_r($_POST);
		//exit;
		
if (!empty($_GET['op'])) {
  if (trim($_GET['op']) =="clone" && intval($_GET['bid'])>0) {
	  clone_block(intval($_GET['bid']));
	} 
} elseif (!empty($_POST['op'])) {
  $bid= intval($_POST['bid']);
	$bsite = (!empty($_POST['bsite'])) ? intval($_POST['bsite']) : 0;
	$bvisible = intval($_POST['bvisible']);
	$bcachetime = $_POST['bcachetime'];
	$bmodule = $_POST['bmodule'];
	$options = $_POST['options'];
	$btitle = $_POST['btitle'];
	clone_block_ok($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options,$btitle);
} else {
  redirect_header(XOOPS_URL."/", 3, _NOPERM);
	exit();
}		
?>